<template>
  <view class="share-container">
    <!-- 组织信息 -->
   <view class="org-info" v-if="orgState.detail">
     <view class="info-item">
       <text class="info-title">{{ orgState.detail.name }}</text>
     </view>
     <view class="info-item">
       <text class="info-label">ID: </text>
       <text class="info-value">{{ orgState.detail.codeNo }}</text>
     </view>
     <view class="info-item">
       <text class="info-label">详细地址: </text>
       <text class="info-value">{{ orgState.detail.address }}</text>
     </view>
    </view>
    <view class="button-section">
      <button class="gvr-yellow-button joinbutton"  @click="handleJoin">加入</button>
    </view>
  </view>
</template>

<script setup>
import { ref,reactive, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { RouterMethods } from '@/utils/router'
import { orgApi,userApi} from '@/service/api'
import { userStore } from '@/utils/userStore.js' 

// 组织状态管理
const orgState = ref({
  detail: null,
  loading: false,
  error: null,
})
// 页面加载
onLoad((options) => {
  const codeNoId = options?.codeNoId || "Y5957d06"
  getOrgDetail(codeNoId)
})

// 获取组织详情
const getOrgDetail = async (codeNoId) => {
  try {
	const result = await orgApi.findOrgByCode(codeNoId)
	if(result.code == undefined) {
	  orgState.value.detail = result
	} else {
	  throw new Error(result.msg || '获取组织详情失败')
	}
  } catch(err) {
    orgState.value.error = err.message
    uni.showToast({
      title: err.message || '获取组织详情失败',
      icon: 'none'
    })
  } finally {
    orgState.value.loading = false
  }
}
// 处理加入
const handleJoin = async () => {
  try {
    if (!orgState.value.detail?.codeNo) {
      uni.showToast({
        title: '组织信息不完整',
        icon: 'none'
      })
      return
    }

    const result = await orgApi.joinOrg(orgState.value.detail.codeNo)
    if (result.code == undefined) {
      // 加入成功
	  let userResult = await getUserInfo()
	  uni.showToast({
	    title: '加入成功', 
	    icon: 'success' ,
		duration:1000
	  })
	  userStore.saveUserInfo(userResult)

      // 延迟跳转，让用户看到提示
      setTimeout(() => {
        RouterMethods.toRepairerCreateShare(result.orgId,false)
      }, 1300)
    } else {
      throw new Error(result.message || '加入失败')
    }
  } catch (err) {
    uni.showToast({
      title: err.message || '加入失败',
      icon: 'none'
    })
  }
}
const getUserInfo = async() => {
	try {
		const result = await userApi.getUserInfo()
		if (result.code == undefined) {
		  return result
		} else {
		  throw new Error(result.msg || '获取用户信息失败')
		}
			   
	} catch (error){
	   uni.showToast({
		 title: error.message || '获取用户信息失败',
		 icon: 'none'
	   })
	}
}
</script>

<style scoped>
.share-container {
  min-height: 100vh;
  background-color: #FFFFFF;
  padding: 0 90rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-sizing: border-box;
}

.org-info {
  width: 100%;
  display: flex;
  flex-direction: column;
  margin-top: 280rpx;
}

.info-item {
  display: flex;
  align-items: flex-start;
  margin-top: 30rpx;
}
.info-title {
  font-family: PingFangSC-Medium;
  font-size: 32rpx;
  color: #000000;
  letter-spacing: 0;
  font-weight: 500;
  width: 100%;
  text-align: center;
}
.info-label {
  font-family: PingFangSC-Regular;
  font-size: 28rpx;
  color: #000000;
  letter-spacing: 0;
  font-weight: 400;
}

.info-value {
  margin-left: 15rpx;
  font-family: PingFangSC-Regular;
  font-size: 28rpx;
  color: #000000;
  letter-spacing: 0;
  font-weight: 400;
  flex: 1;
}

.button-section {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
  background-color: #FFFFFF;
}

.joinbutton {
  border-radius: 0;
}
</style>